import {Notification} from "element-ui";
import store from "@/store";

//ws消息策略
class MsgStrategy {
  execute(msg) {
    console.log("execute msg"+msg);
  }
}

/**
 * 默认
 */
class DefaultStrategy extends MsgStrategy {
  execute(msg) {
    store.commit('SET_WS_MSG', JSON.stringify(msg));
  }
}

/**
 * 强制下线
 */
class OfflineStrategy extends MsgStrategy {
  execute(msg) {
    Notification({
      title: '强制下线提醒',
      type: 'warning',
      dangerouslyUseHTMLString: true,
      message: '强制下线提醒',
      offset: 60,
    })
  }
}


/**
 * 映射关系
 * @type {{offline: OfflineStrategy, defaultMsg: DefaultStrategy}}
 */
const strategyMap = {
  defaultMsg: DefaultStrategy,
  offline: OfflineStrategy,
};

export class WSMsgContext{
  handleMsg(msg) {
    let msgType = msg&&msg.type?(msg.type):'defaultMsg';
    const s = strategyMap[msgType];
    s.prototype.execute(msg)
  }
}


